<?php
class NotifyService extends Think{
    public function __construct($data) {
    }

	/**
	 * 用户对用户发送通知
	 * @param string|int|array $receive 接收人ID 多个时以英文的","分割或传入数组
	 * @param string           $type    通知类型, 必须与模版的类型相同, 使用下划线分割应用.
	 * 					   				如$type = "weibo_follow"定位至/apps/weibo/Language/cn/notify.php的"weibo_follow"
	 * @param array            $data
	 * @param int              $from    发送人ID
	 * @return void
	 */
	public function send( $receive , $type , $data  , $from ) {
		return $this->__put( $receive , $type , $data , $from );
	}

	/**
	 +----------------------------------------------------------
	 * Description 通知发送处理
	 +----------------------------------------------------------
	 +----------------------------------------------------------
	 * @param $type    通知类型
	 * @param $receive 通知接收者的用户ID,类型可为 数字、字符串、数组
	 * @param $title   通知标题
	 * @param $body    通知内容
	 * @param $from    通知发送者UID
	 * @param $system  是否为系统通知
	 +----------------------------------------------------------
	 * @return Boolen
	 +----------------------------------------------------------
	 */
	private function __put($receive,$type,$data,$from=0,$system=false) {
		global $yl;
		$receive = $this->_parseUser( $receive ); if(!$receive) return false;
		$from = ( $system==false &&  $from==0 ) ? $yl['user']['uid'] : $from ;
		$data      = addslashes(serialize( $data ));
		$time       = time();

		//优化大批量发送通知，讲数据切割处理，每次插入100条
		$receive	=	array_chunk($receive, 100)  ;
		foreach ($receive as $receive_chunck){

			foreach ($receive_chunck as $k=>$v){
				if($v==$from) continue;
				$sqlArr[] = "($from,$v,'$type','$data',$time)";
			}

			if( $sqlArr ){
				$sql = "INSERT INTO ".C('DB_PREFIX')."notify (`from`,`receive`,`type`,`data`,`ctime`) values ".implode(',',$sqlArr);
				$result[] = M('Notify')->execute($sql);

			}

			unset($sql,$sqlArr,$receive_chunck);
		}

		return $result;
	}

	//解析传入的用户ID
	private function _parseUser($touid){
		if( is_numeric($touid) ){
			$sendto[] = $touid;
		}elseif ( is_array($touid) ){
			$sendto = $touid;
		}elseif (strpos($touid,',') !== false){
			$touid = array_unique(explode(',',$touid));
			foreach ($touid as $key=>$value){
				$sendto[] = $value;
			}
		}else{
			$sendto = false;
		}
		return $sendto;
	}
	
	/**
	 * 获取给定用户的通知统计
	 * @param int $mid
	 * @return array 格式为:
	 *               <code>
	 *               array(
	 *               	'message'		=> '0', // 未读短消息数
	 *               	'notify'		=> '0', // 未读通知数
	 *               	'comment'		=> '0', // 未读评论总数
	 *               	'atme'			=> '0', // 未读的@我的总数
	 *               	'total'			=> '0', // 以上未读的总数
	 *               	'weibo_comment' => '0', // 未读的微博评论数
	 *               	'global_comment'=> '0', // 未读的其它应用评论数
	 *               )
	 *               </code>
	 */
	public function getCount($uid) {
		$uid = intval($uid);
		if ($uid <= 0) {
			return array(
					'message'		 => 0,
					'notify'		 => 0,
					'comment'		 => 0,
					'atme'			 => 0,
					'total'			 => 0,
					'weibo_comment'	 => 0,
					'global_comment' => 0,
			);
		}
		$weibo_count	= model('UserCount')->where('uid='.$uid)->find();
		$global_comment = intval(model('Message')->getUnreadCount($uid));
		$return['message']    		= model('Message')->getUnreadMessageCount($uid);
		$return['notify']     		= M('notify')->where('receive='.$uid.' AND is_read=0')->count();
		$return['comment']    		= intval( $weibo_count['comment'] ) + $global_comment;
		$return['atme']       		= intval( $weibo_count['atme'] );
		$return['weibo_comment']	= intval($weibo_count['comment']);
		$return['global_comment']	= $global_comment;
		$return['total']      		= array_sum($return);
		return $return;
	}
}
?>